/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package layoutHandling;

import java.io.IOException;
import java.io.StringReader;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 *
 * @author Roxana Radulescu
 */
public class XMLParser {

    private String xml;
    private LayoutHandler lh;

    public XMLParser() {
        lh = LayoutHandler.getInstance();
    }

    public XMLParser(String xml) {
        lh = LayoutHandler.getInstance();
        this.xml = xml;
    }

    public LayoutHandler getLh() {
        return lh;
    }

    public String getXml() {
        return xml;
    }

    public void setLh(LayoutHandler lh) {
        this.lh = lh;
    }

    public void setXml(String xml) {
        this.xml = xml;
    }

    public void parseXML() {

        try {

            //create an inputstream from the string so it can be parsed into the dom
            InputSource inStream = new InputSource();
            inStream.setCharacterStream(new StringReader(xml));


            //import the xml string
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();


            Document doc = builder.parse(inStream);
            NodeList nList = doc.getElementsByTagName("cael");
            Node n = nList.item(0);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element) n;
                lh.putValue("aeh", e.getAttribute("aeh"));

            }
            nList = doc.getElementsByTagName("countryCode");
            n = nList.item(0);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element) n;
            }
            nList = doc.getElementsByTagName("authority");
            n = nList.item(0);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element) n;

                lh.putValue("a_help", e.getAttribute("help"));
                lh.putValue("expert", e.getAttribute("expert"));
                lh.putValue("aeh_approved", e.getAttribute("aehApproved"));
            }
            nList = doc.getElementsByTagName("group");
            n = nList.item(0);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element) n;
                lh.putValue("gender_separation", e.getAttribute("genderseparation"));
            }
            nList = doc.getElementsByTagName("language");
            n = nList.item(0);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element) n;
                lh.putValue("presence", e.getAttribute("presence"));
                lh.putValue("culture", e.getAttribute("culture"));
            }
            nList = doc.getElementsByTagName("lesson");
            n = nList.item(0);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element) n;
                lh.putValue("hierarchy", e.getAttribute("hierarchy"));
                lh.putValue("l_help", e.getAttribute("help"));
                lh.putValue("access", e.getAttribute("access"));
                lh.putValue("choice", e.getAttribute("choice"));
                lh.putValue("ambiguity", e.getAttribute("ambiguity"));

            }
            nList = doc.getElementsByTagName("data");
            n = nList.item(0);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element) n;
                lh.putValue("security", e.getAttribute("security"));
            }


        } catch (SAXException ex) {
            Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, ex);
        }


    }
}
